
#include "stdafx.h"
#include "Socket.h"

MCSocket::MCSocket() 
{
	this->m_socket = SOCKET_ERROR ;

	WSADATA wsaData;
	if ( WSAStartup(MAKEWORD(2,2), &wsaData))
		ExitProcess (0) ;
}

MCSocket::~MCSocket()
{
	WSACleanup();
}

bool	MCSocket::Create ( int type , int protocol )
{
	this->m_socket = socket ( AF_INET , type , protocol );
	if ( this->m_socket == INVALID_SOCKET )	
		return false ;
	return true ;
}

bool	MCSocket::Close()
{
	if ( ::closesocket ( this->m_socket ))
		return false ;
	return true ;
}

bool	MCSocket::Bind( u_short port )
{
	sockaddr_in	addr ;
	addr.sin_family = AF_INET ;
	addr.sin_port = port;
	addr.sin_addr.S_un.S_addr = INADDR_ANY;
	if ( ::bind ( this->m_socket , (SOCKADDR *) &addr , sizeof (addr) ) )
		return false ;
	return true ;
}

bool	MCSocket::Listen(int n)
{
	if (::listen ( this->m_socket , n )) 
		return false ;
	return true ;
}

bool	MCSocket::Accept( sockaddr_in& addr)
{
	int	len = sizeof (addr);
	SOCKET s = ::accept ( this->m_socket , (SOCKADDR *)&addr , &len );
	if ( s == INVALID_SOCKET )
		return false ;
	return true ;
}

bool	MCSocket::Accept(char *saddr, int &port) 
{
	sockaddr_in	addr ;
	bool	bResult = this->Accept( addr ) ;
	
	port = addr.sin_port ;
	sprintf_s ( saddr , 16 , "%d.%d.%d.%d" , 
				addr.sin_addr.s_net , 
				addr.sin_addr.s_host , 
				addr.sin_addr.s_lh , 
				addr.sin_addr.s_impno ) ;
	return bResult ;
}

bool	MCSocket::Connect( const char* saddr , u_short port )
{
	sockaddr_in	addr ;
	addr.sin_family = AF_INET ;
	addr.sin_port = port;
	addr.sin_addr.s_addr = inet_addr(saddr);
	if ( ::connect ( this->m_socket , (SOCKADDR *)&addr , sizeof (addr)) == SOCKET_ERROR )
		return false ;
	return true ;
}

int	MCSocket::Send ( const char* buffer , int len , int flags )
{
	int	nResult = ::send ( this->m_socket , buffer , len , flags ) ;
	if ( SOCKET_ERROR == nResult ) 
		throw "Recv Error!" ;
	return nResult ;
}

int	MCSocket::Recv( char*buffer , int len , int flags ) 
{
	int	nResult = ::recv ( this->m_socket , buffer , len , flags ) ;
	if ( SOCKET_ERROR == nResult ) 
		throw "Recv Error!" ;
	return nResult ;
}

int	MCSocket::SendTo( const char* saddr , int Port , char* buffer , int len , int flags ) 
{
	int nResult=0;
	if (saddr&&saddr[0])
	{
		sockaddr_in	RecvAddr ;
		RecvAddr.sin_family = AF_INET;
		RecvAddr.sin_port = Port;
		RecvAddr.sin_addr.s_addr = inet_addr(saddr);
		nResult = sendto ( this->m_socket ,
						buffer , 
						len , 
						flags , 
						(SOCKADDR *)&RecvAddr , 
						sizeof (RecvAddr)) ;
		if ( SOCKET_ERROR == nResult )
			throw "SendTo Error !" ;
	}
	return nResult ;
}

int	MCSocket::RecvFrom( char* saddr , int& Port , char* buffer , int len , int flags )
{
	sockaddr_in	SendAddr ;
	SendAddr.sin_family = AF_INET ;
	SendAddr.sin_port =Port;
	int	fromsize = sizeof (SendAddr);
	int	nResult = recvfrom ( this->m_socket , 
				buffer , 
				len , 
				flags , 
				(SOCKADDR *)&SendAddr , 
				&fromsize);
	if ( SOCKET_ERROR == nResult )
		throw "RecvFrom error !" ;
	Port = SendAddr.sin_port ;
	sprintf_s ( saddr , 16 , "%d.%d.%d.%d" , 
				SendAddr.sin_addr.s_net , 
				SendAddr.sin_addr.s_host , 
				SendAddr.sin_addr.s_lh , 
				SendAddr.sin_addr.s_impno ) ;
	return nResult ;
}

CString	MCSocket::GetLocalIp()   
{   
    struct hostent * phost;   
    char ip[20];   
    char hostname[50];   
       
    gethostname(hostname, 50);   
    phost = gethostbyname(hostname);   
    char * * names;   
    names = phost->h_aliases;   
    char * * iplist;   
    iplist = phost->h_addr_list;   
    while (* iplist)   
    {   
        strcpy_s (ip, 20 , inet_ntoa(* (struct in_addr *) * iplist));   
        iplist++;   
    }   
       
    CString LocalIp = ip;   
    return LocalIp;   
}   
